/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.sartori_daniello_picca.meteocal.entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import static javax.persistence.TemporalType.DATE;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Ale
 */
@Entity
@XmlRootElement
@NamedQueries({
    @NamedQuery (name= "Notification.findByAll", query= "SELECT n FROM Notification n"),
    @NamedQuery (name= "Notification.findById", query= "SELECT n FROM Notification n WHERE n.id = :id"),
    @NamedQuery (name= "Notification.findByNotificationType", query= "SELECT n FROM Notification n WHERE n.notificationtype  = :notificationtype"),
    @NamedQuery(name = "Notification.findByReceiver", query = "SELECT n FROM Notification n WHERE n.receiver = :receiver"),
    @NamedQuery(name = "Notification.advancedResearch", query = "SELECT n FROM Notification n WHERE n.receiver = :receiver AND n.notificationtype= :notificationtype AND n.eventassociated= :eventassociated")
})
public class Notification implements Serializable {
    
    public static final String findByAll = "Notification.findByAll";
    public static final String findById = "Notification.findById";
    public static final String findByNotificationType = "Notification.findByNotificationType";
    public static final String findByReceiver = "Notification.findByReceiver";
    public static final String advancedResearch= "Notification.advancedResearch";
    public static final long serialVersionUID = 1L;
    
    @Basic(optional = false)
    @NotNull
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name= "Id")
    @Id
    private Long id;
    
    @Pattern(regexp="^[a-zA-Z0-9 ]*$",
            message="Invalid Character")
    @Size(max = 255)
    @Column(name= "Message")
    private String message;
    
    @NotNull
    @JoinColumn (name="Eventassociated")
    private Event eventassociated;
    
    @Column(name="Seenbyuser")
    private Boolean seenbyuser;
  
    @NotNull (message ="May not be empty")
    @Column (name="Notificationtype")
    private NotificationType notificationtype;
    
    @Temporal(DATE)
    @Column (name="Firstgoodday")
    private Date firstgoodday;
    
    
    @ManyToOne
    @JoinColumn(name="Receiver", referencedColumnName="Username")
    private Users receiver;
    
    public Notification() {
    }
            
     public Notification(Long id) {
        this.id = id;
    }

    public Notification(Long id, String message, Boolean seenbyuser, Event eventassociated, NotificationType notificationtype, Date firstgoodday, Users receiver) {
        this.id = id;
        this.message = message;
        this.seenbyuser = seenbyuser;
        this.eventassociated= eventassociated;
        this.notificationtype = notificationtype;
        this.firstgoodday= firstgoodday;
        this.receiver=receiver;
    }
    
    public Event getEventAssociated () {
        return eventassociated;
    }
    
    public void setEventAssociated (Event eventassociated) {
        this.eventassociated=eventassociated;
    }
    
    public void setFirstGoodDay (Date firstgoodday) {
        this.firstgoodday= firstgoodday;
    }
    
    public Date getFirstGoodDay () {
        return firstgoodday;
    }
    
    public String getFirstGoodDayMsg() {
     if (firstgoodday==null) {
            return ("Sorry, there aren't days with good weather");
    }
     else{
         return(firstgoodday.getDate()+":"+(firstgoodday.getMonth()+1)+":"+(firstgoodday.getYear()+ 1900));
     }
    }
    
    public void setReceiver (Users receiver) {
        this.receiver = receiver;
    }
    
    public Users getReceiver () {
        return receiver;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    public Boolean getSeenByUser () {
        return seenbyuser;
    }
    
    public void setSeenByUser (Boolean seenbyuser) {
        this.seenbyuser = seenbyuser;
    }
    
    public NotificationType getNotificationType() {
        return notificationtype;
    }
    
    public void setNotificationType(NotificationType notificationtype){
            this.notificationtype= notificationtype;
    }
    
    public enum NotificationType {
        
        WEATHER3DAY,
        WEATHER1DAY,
        INVITATION,
        FRIENDSHIP,
        MODIFICATION,
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Notification)) {
            return false;
        }
        Notification other = (Notification) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "it.polimi.sartori_daniello_picca.meteocal.entity.Notification[ id=" + id + " ]";
    }

}

